package com.hqd.ch03.v46.utils;

import com.hqd.ch03.v46.beans.BeansException;
import com.hqd.ch03.v46.factory.ListableBeanFactory;

import java.util.LinkedHashMap;
import java.util.Map;

public abstract class BeanFactoryUtils {
    public static String[] beanNamesForTypeIncludingAncestors(
            ListableBeanFactory beanFactory, Class<?> type) {
        return beanFactory.getBeanNamesForType(type);
    }

    public static <T> Map<String, T> beansOfTypeIncludingAncestors(
            ListableBeanFactory lbf, Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
            throws BeansException {

        Map<String, T> result = new LinkedHashMap<>(4);
        String[] beanNamesForType = lbf.getBeanNamesForType(type);
        for (String bnft : beanNamesForType) {
            result.put(bnft, (T) lbf.getBean(bnft));
        }
        return result;
    }
}
